<?php
/*
PWision extension: Newspaper http://pw-newspaper.googlecode.com/
Copyright (C) 2010 Becheru Petru-Ioan

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
include_once("inc/PRedirectSection.inc");
include_once("pw-newspaper.inc");
/** 	The RandomArticle class is designed to model a section that redirects to a random article.
	*	\latexonly  \label{PWision:Extensions:Newspaper:RandomArticle} \index{extension} \index{newspaper} \index{article} \endlatexonly
	*	\brief go to a random article.
	*	\version 1.0.1
	*/
class RandomArticle extends PRedirect2Section
	{
	function RandomArticle()
		{
		$ID=1;
		$maxID= Main()->DB->fetch_array_query('SELECT Max(ID) `maxID` FROM `NArticle`');
		$maxID=$maxID['maxID'];
		for($Try=256;$Try--;)
			{
			$ID=rand(1,$maxID);
			if(Main()->DB->fetch_array_query("select ID from `NArticle` where ID='$ID'"))
				break;// if the random article was found
			$ID=1;// if the random articole was not found
			}
		PRedirect2Section::PRedirect2Section(
			"Article"
			, Object(new PURLAttributes)
					->setAtr('ID',$ID)
			);
		}
	}

?>